#!/bin/bash

# install Neovim
if [ -z "$(command -v nvim)" ]; then 
  read -r -p "Install NeoVim Latest automatically ?[Y/N]" input
  case $input in
    [yY][eE][sS]|[yY])
      wget https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz
      tar -xvf nvim-linux64.tar.gz -C /usr/local/
      ln -s /usr/local/nvim-linux64/bin/nvim /usr/local/bin/nvim
      ;;

    [nN][oO]|[nN]|*)
      echo "Not Install NeoVim automatically!"
      exit 1
      ;;
  esac
fi


# create alias
read -r -p "Create vi/vim alias nvim ?[Y/N]" input
case $input in
  [yY][eE][sS]|[yY])
    if [ $SHELL == "/bin/zsh" ]; then
      env_file=".zshrc"
    else
      env_file=".bashrc"
    fi

    if [ `grep -c -E "^alias vi=" ~/${env_file}` -gt '0' ]; then
      sed -i "s/^alias\ vi=.*/alias vi=\"nvim\"/g" ~/${env_file}
    else
      echo "alias vi=\"nvim\"" >> ~/${env_file}
    fi
    if [ `grep -c -E "^alias vim=" ~/${env_file}` -gt '0' ]; then
      sed -i "s/^alias\ vim=.*/alias vim=\"nvim\"/g" ~/${env_file}
    else
      echo "alias vim=\"nvim\"" >> ~/${env_file}
    fi

    source ~/${env_file}
    ;;

  [nN][oO]|[nN]|*)
    echo "Not Create Alias!"
    ;;
esac

nvim +'hi NormalF:loat guibg=#1e222a' +PackerSync
